package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.entity.Teachlink;
import com.lanou3g.ptp.teachlink.service.TeachlinkService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "教学环节")
@RestController
public class TeachlinkController {

    @Reference
    private TeachlinkService teachlinkService;

    @ApiOperation("查询所有教学环节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "数量"),
            @ApiImplicitParam(name = "keyword", value = "关键字")
    })
    @GetMapping("/teachlinks")
    public PtpResult find(@RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "30") Integer size,
                          String keyword) {
        //分页  自己百度pageInfo
        PageInfo<Teachlink> p;
        try {
            if (keyword != null && !keyword.trim().equals("")) {
                p = teachlinkService.findByKeyword(page,size,keyword);

            } else {
                p = teachlinkService.findAll(page,size);
            }
            return PtpResultUtils.ok(p);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("根据id查询教学环节")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教学环节id")
    })
    @GetMapping("/teachlink/{id}")
    public PtpResult findById(@PathVariable Long id) {

        try {
            Teachlink byId = teachlinkService.findById(id);
            return PtpResultUtils.ok(byId);
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("添加教学环节")
    @PostMapping("/teachlink")
    public PtpResult insert(Teachlink teachlink) {
        try {
            teachlinkService.insert(teachlink);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教学环节id")
    })
    @DeleteMapping("/teachlink/{id}")
    public PtpResult delete(@PathVariable Long id) {
        try {
            teachlinkService.delete(id);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("根据id删除多个")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "教学环节id数组")
    })
    @DeleteMapping("/teachlinks/{ids}")
    public PtpResult deletes(@PathVariable Long[] ids) {
        try {
            teachlinkService.deletes(ids);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }

    @ApiOperation("根据id修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "教学环节id")
    })
    @PutMapping("/teachlink/{id}")
    public PtpResult update(@PathVariable Long id, Teachlink teachlink) {
        try {
            teachlinkService.update(id, teachlink);
            return PtpResultUtils.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return PtpResultUtils.error(500);
        }
    }
}
